﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Test_Case_Marker
{
    /// <summary>
    /// Interaction logic for Show_Test_Cases.xaml
    /// </summary>
    public partial class Show_Test_Cases : Window
    {

        private MainWindow main_wind = null;
        public Show_Test_Cases()
        {
            InitializeComponent();
            main_wind = null;
        }

        public void Cast_Obj(MainWindow wind)
        {
            main_wind = wind;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mark_result.IsEnabled = true;
            mark_result.Items.Add("Pass");
            mark_result.Items.Add("Fail");
            mark_result.Items.Add("Blocked");
           
        }

        

        private void save_Click(object sender, RoutedEventArgs e)
        {

            if (TC_DataGrid.SelectedItems.Count == 0)
                MessageBox.Show("Please select one or more test cases");
            else
            {
                int[] id_list = new int[TC_DataGrid.SelectedItems.Count];
                int counter = 0;
                foreach (var sel_data in TC_DataGrid.SelectedItems)
                {
                    Test_Case Selected_TC = sel_data as Test_Case;
                    id_list[counter++] = Selected_TC.ID;


                }

                ITestPointCollection TestPoints = MainWindow.TP.QueryTestPoints(string.Format("Select * from TestPoint where TestCaseId in ( {0} )", string.Join(",", id_list)));
                ITestRun run = MainWindow.TP.CreateTestRun(false);
                run.Owner = MainWindow.test_service.AuthorizedIdentity;
                foreach (ITestPoint testpoint in TestPoints)
                {
                    run.AddTestPoint(testpoint, null);
                }
                run.Save();

                ITestCaseResultCollection results = run.QueryResults();
                foreach (ITestCaseResult result in results)
                {

                    result.RunBy = run.Owner;
                    string outcome = mark_result.SelectedValue.ToString();
                    result.Outcome = outcome.Equals("Pass") ? TestOutcome.Passed : outcome.Equals("Fail") ? TestOutcome.Failed : TestOutcome.Blocked;
                    result.State = TestResultState.Completed;
                    result.Save();
                }
                MessageBox.Show("Selected Test Case(s) have been executed successfully");
            }
        
        }

        private void Main_Click(object sender, RoutedEventArgs e)
        {
            
            MessageBoxResult result = MessageBox.Show("Are you sure you want to return to the main window?", "Return to Main Window", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                this.Close();
                main_wind.Show();
               
            }
            else 
                return;
 
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            main_wind.Show();
        }

      
       

       

        

    }
}
